package com.ruoyi.web.controller.api;

import com.ruoyi.cat.domain.CatShareuserTable;
import com.ruoyi.cat.domain.CatUserbasicTable;
import com.ruoyi.cat.domain.VO.ApiSaveShareVO;
import com.ruoyi.cat.domain.VO.ReferralCodeVO;
import com.ruoyi.cat.service.ICatUserbasicTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.WxLoginUser;
import com.ruoyi.framework.web.service.WxTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "api-用户相关接口")
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

    @Autowired
    private WxTokenService wxTokenService;

    @Autowired
    private ICatUserbasicTableService userbasicTableService;


    @ApiOperation("api-根据推荐码绑定用户")
    @PostMapping("referralCode")
    public AjaxResult referralCode(HttpServletRequest request, @Validated @RequestBody ReferralCodeVO referralCodeVO) {
        CatUserbasicTable catUserbasicTable = userbasicTableService.selectCatUserbasicTableByReferenceNum(referralCodeVO.getCode());
        CatUserbasicTable catShareuserTable = new CatUserbasicTable();
        if (catUserbasicTable == null) {
            return error("推荐码不存在");
        }
        WxLoginUser wxLoginUser = wxTokenService.getWxUser(request);
        CatUserbasicTable catUserbasicTable1 = userbasicTableService.selectCatUserbasicTableById(wxLoginUser.getUserId());
        if (catUserbasicTable1 != null && catUserbasicTable1.getReferenceId() != null) {
            return error("您已经绑定过推荐码");
        }
        catShareuserTable.setId(wxLoginUser.getUserId());
        catShareuserTable.setReferenceId(catShareuserTable.getId());
        return success(userbasicTableService.updateCatUserbasicTable(catShareuserTable));
    }



}
